package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.operation.DeleteServiceHelper;

/**
 * 拼车车主注册
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobDriverdetail extends AbstractFormPlugin {

	private String[] buttons = new String[] { "return" ,"submit"};

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		IFormView view = this.getView();
		FormShowParameter parameter = view.getFormShowParameter();
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		if (StringUtils.equals("return", key)) {//返回
			Map<String, Object> formId = new HashMap<>();
			String parentId = parameter.getParentFormId();
			formId.put("formId", parentId);
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		} else if (StringUtils.equals("submit", key)) {//提交
			long userId = Long.parseLong(RequestContext.get().getUserId());
			QFilter filterdelete = new QFilter("creator", QCP.equals, userId);
			filterdelete.and(new QFilter("billstatus", QCP.equals, "Z"));
			//删除上一条数据
			int cc = DeleteServiceHelper.delete("kdec_pincardetailbill", filterdelete.toArray());
			view.setVisible(false, "flexpanelap");
			view.setVisible(false, "submit");
			view.setVisible(true, "shflex");
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		IDataModel model = this.getModel();
		DynamicObject creator = (DynamicObject)model.getValue("creator");
		model.setValue("name", creator.getString("name"));
		model.setValue("telephone", creator.getString("phone"));
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
	}
}
